var pile = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: jsctx + "/pileMonitor/listPagePile",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 10,
                    pageList: [10, 20, 30],
                    // pageList: [10],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    rightFixedColumns: true,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,

                            stationName: $("#stationName").val(),
                            pileNo: $("#pileNo").val(),
                            pileName: $("#pileName").val(),
                            status: $("#status").val()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'pileNo', title: '电桩编号', align: 'center', width: '100'},
                        {field: 'pileName', title: '设备编号', align: 'center', width: '100'},
                        {field: 'pileSort', title: '设备顺序号', align: 'center', width: '100'},
                        {
                            field: 'status', title: '服务状态', width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                if (row.pileStatus == 0 || value == 4) {
                                    return '<span class="label label-info">离线</span>'
                                } else {
                                    if (row.normalStatus != 0) {
                                        return '<span class="label label-info">故障</span>'
                                    }
                                    if (value == 0) {
                                        return '<span class="label label-info">空闲</span>'
                                    } else if (value == 1) {
                                        return '<span class="label label-danger">使用中</span>'
                                    } else if (value == 2) {
                                        return '<span class="label label-danger">插抢中</span>'
                                    }
                                }
                            }
                        },
                        {field: 'stationName', title: '所属站点', align: 'center', width: '100'},
                        {field: 'operatorName', title: '所属加盟商', align: 'center', width: '100'},

                        {field: 'voltage', title: '电压(V)', align: 'center', width: '100'},

                        {field: 'rateElec', title: '电流(A)', align: 'center', width: '100'},
                        {field: 'temperature', title: '温度(℃)', align: 'center', width: '100'},
                        {field: 'ratePower', title: '功率(KW)', align: 'center', width: '100'},

                        {field: 'orderTime', title: '满电时间', align: 'center', width: '100'},
                        {field: 'occupyTime', title: '占桩时间', align: 'center', width: '100'},

                        {field: 'soc', title: 'SOC', align: 'center', width: '100'},

                        {field: 'bmsVoltage', title: 'BMS电压', align: 'center', width: '100'},
                        {field: 'bmsElec', title: 'BMS电流', align: 'center', width: '100'},
                        {field: 'bmsTemperature', title: 'BMS温度', align: 'center', width: '100'},

                        {
                            field: 'status', title: '状态', width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                if (row.status != null) {
                                    if (row.status == '0') {
                                        return "<input type='checkbox' id='enterprise-status' name='enterprise-status' value=" + row.status + " checked>";
                                    } else {
                                        return "<input type='checkbox' id='enterprise-status' name='enterprise-status' value=" + row.status + " >";
                                    }
                                } else {
                                    return null;
                                }

                            }
                        },
                        {field: 'totalMoney', title: '金额', align: 'center', width: '100'},
                        {field: 'orderUpdateTime', title: '更新时间', align: 'center', width: '100'},

                    ], onLoadSuccess: function () {
                        initSwitch();
                    },

                });
    }

    function reLoad() {
        $(".fixed-table-header-columns").css("display", "none");
        $(".fixed-table-body-columns").css("display", "none");
        $('#exampleTable').bootstrapTable('refresh');
    }

    function addElecPileByStation(elecStationId) {
        location.href = jsctx + "/elecStation/addElecPileByStation/" + elecStationId;
    }

    function deleteElecPile(elecPileId) {
        layer.confirm('确定要删除选中的记录(对应枪同时被删除)？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteElecPile",
                type: "post",
                data: {
                    'elecPileId': elecPileId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function initSwitch() {

        $("[name='enterprise-status']").bootstrapSwitch({
            onText: "重启",      // 设置ON文本
            offText: "停用",    // 设置OFF文本
            onColor: "primary",// 设置ON文本颜色(info/success/warning/danger/primary)
            offColor: "warning",  // 设置OFF文本颜色 (info/success/warning/danger/primary)
            size: "mini",    // 设置控件大小,从小到大  (mini/small/normal/large)
            hadleWidth: "10",
            // 当开关状态改变时触发
            onSwitchChange: function (event, state) {
                layer.msg("桩暂时不支持");
                //修改操作
                // $.ajax({
                //     type: "post",
                //     dataType: "json",
                //     async: false,
                //     url: jsctx + "/enterpriseMember/updateEnterpriseStatus",
                //     data: {
                //         status: state ? 0 : 1,
                //         id: this.value
                //     },
                //     success: function (data) {
                //         $('#TestTables').bootstrapTable('refresh'); //Table页面刷新
                //     },
                //     error: function (xhr, error, exception) {
                //     }
                // });
            }
        })
    }

    function goBack() {
        window.history.back();
    }

    function printElecGunQrCode(gunNo) {
        layer.open({
            type: 2,
            title: '枪号：' + gunNo,
            shadeClose: false,
            area: ['400px', '350px'],
            content: prefix + '/genElecGunQrCode/' + gunNo,
            //弹窗下方的按钮
            // btn: ['下载'],
            // //绑定第一个按钮的点击事件
            // btn1: function (gunNo) {
            //     genElecGunQrCodeUrl(gunNo)
            //     var url = document.getElementById("qrcode").childNodes[0].toDataURL("image/png");
            //     //下载图片到本地
            //     var a = document.createElement('a');
            //     var event = new MouseEvent('click');
            //     a.download = gunNo;
            //     a.href = url;
            //     a.dispatchEvent(event);
            // }
        });
    }

    function editElecGun(elecGunId) {
        location.href = prefix + "/editElecGun/" + elecGunId;
    }

    function exportInfo() {
        $.fileDownload(jsctx + "/pileMonitor/exporListStationAndLatiLong", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text(),

                stationName: $("#stationName").val(),
                pileNo: $("#pileNo").val(),
                pileName: $("#pileName").val(),
                status: $("#status").val()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,

        deleteElecPile: deleteElecPile,

        addElecPileByStation: addElecPileByStation,
        printElecGunQrCode: printElecGunQrCode,
        editElecGun: editElecGun,
        exportInfo: exportInfo,
    };
})();

var addElecPile = (function () {
    function init() {
        validateRule();
    };


    var isajaxsend = true;


    return {
        init: init
    };
})();
